package com.shiguiwu.springboot3.interceptor;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description: mybatis拦截器
 * @author: stone
 * @date: Created by 2023/7/13 22:08
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.interceptor
 */
@Component
@Intercepts({ @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }) })
public class MybatisInterceptor  implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        Object arg = args[1];

        if (arg instanceof MapperMethod.ParamMap) {
            MapperMethod.ParamMap paramMap = (MapperMethod.ParamMap) arg;

            Object listA = paramMap.get("list");

            if (null == listA || (listA instanceof List<?>
                    && ((List<?>) listA).size() == 0)) {
                return 0;
            }

        }


        return invocation.proceed();
    }
}
